package ltd.dujiabao.springcloud.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.springcloud.entities.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author : DuJiabao
 * @Project : springcloud
 * @Time : 2021/5/21 15:51
 * @Desc :
 */
@Api("Discovery")
@Slf4j
@RestController
@RequestMapping("/discovery")
public class DiscoveryController {
  @Autowired
  private DiscoveryClient discoveryClient;

  @GetMapping("/allService")
  public CommonResult<List<String>> allService() {
    List<String> services = discoveryClient.getServices();
    log.info("Eureka包含的服务包括：" + services.toString());
    return new CommonResult<>(CommonResult.SUCCESS, "获取所有服务成功！", services);
  }

  @GetMapping("/allInstances/{serviceName}")
  public CommonResult<List<ServiceInstance>> allInstances(@PathVariable("serviceName") String serviceName) {
    List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
    log.info("CLOUD-PAYMENT-SERVICE 包含的实例包括：" + instances.toString());
    return new CommonResult<>(CommonResult.SUCCESS, "获取所有实例成功！", instances);
  }
}
